package org.luxor.common.netty.protocol;

import cn.hutool.core.util.StrUtil;

import java.nio.charset.Charset;

/**
 * Netty Content Type
 *
 * @author Mr.yan @date 2024/2/21
 */
public enum NettyContentType {

    /**
     * 二进制流编码
     */
    STREAM("application/octet-stream"),

    /**
     * JSON编码
     */
    JSON("application/json"),

    /**
     * text/plain编码
     */
    TEXT_PLAIN("text/plain");

    private final String value;

    NettyContentType(String value) {
        this.value = value;
    }

    public String getValue() {
        return value;
    }

    @Override
    public String toString() {
        return getValue();
    }

    /**
     * 输出Content-Type字符串，附带编码信息
     *
     * @param charset 编码
     * @return Content-Type字符串
     */
    public String toString(Charset charset) {
        return build(this.value, charset);
    }

    public static String build(String contentType, Charset charset) {
        return StrUtil.format("{};charset={}", contentType, charset.name());
    }
}
